/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = __tonglinkq_analysis_file.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/03/30 by jikw                                 */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include <tonglinkq/tonglinkq_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "frbyte_buffer.h"
#include "../../../src/module/tonglinkq/config.h"

extern int __tonglinkq_analysis_file(L7session_tonglinkq_private *p_st_private
                                   , Frreport_body_data *p_st_body_data
                                   , Frbyte_Packet *p_st_data);

static void read_file(char *p_filename, char **p_out, int *p_i_len)
{
    FILE *p_file = NULL;
    //struct stat buf;
    char *p_data = NULL;
    int i_len = 0;

    p_file = fopen(p_filename, "r");
    fseek(p_file, 0, SEEK_END);
    i_len = ftell(p_file);
    rewind(p_file);

    p_data = (char *)frmalloc(i_len + 1);
    memset(p_data, 0x00, i_len + 1);
    fread(p_data, 1, i_len, p_file);
    fclose(p_file);

    *p_out = p_data;
    *p_i_len = i_len;

    return;
}

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    L7session_tonglinkq_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = FR_SUCC;

    p_st_data = (Frbyte_Packet *)frmalloc(sizeof(Frbyte_Packet));
    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));

    i_ret = __tonglinkq_analysis_file(p_st_private, p_st_body_data, p_st_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_data);
    frfree(p_st_body_data);

    return;
}

static void case2(void)
{
    L7session_tonglinkq_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = FR_SUCC;

    p_st_data = (Frbyte_Packet *)frmalloc(sizeof(Frbyte_Packet));
    p_st_private = (L7session_tonglinkq_private *)frmalloc(sizeof(L7session_tonglinkq_private));

    i_ret = __tonglinkq_analysis_file(p_st_private, p_st_body_data, p_st_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_private);
    frfree(p_st_data);

    return;
}

static void case3(void)
{
    L7session_tonglinkq_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frbyte_Packet *p_st_data = NULL;
    int i_ret = FR_SUCC;

    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    p_st_private = (L7session_tonglinkq_private *)frmalloc(sizeof(L7session_tonglinkq_private));

    i_ret = __tonglinkq_analysis_file(p_st_private, p_st_body_data, p_st_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_private);
    frfree(p_st_body_data);

    return;
}

//FILE 消息消息长度0)
static void case4(void)
{
    L7session_tonglinkq_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char *p_data = NULL;
    int i_len = 0;
    int i_ret = FR_SUCC;
    Tonglinkq_L7session *p_st_L7_session = NULL;

    p_st_private = (L7session_tonglinkq_private *)frmalloc(sizeof(L7session_tonglinkq_private));
    memset(p_st_private, 0x00, sizeof(L7session_tonglinkq_private));
    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_L7_session = (Tonglinkq_L7session *)frmalloc(sizeof(Tonglinkq_L7session));
    memset(p_st_L7_session, 0x00, sizeof(Tonglinkq_L7session));
    p_st_private->p_st_L7_session = p_st_L7_session;

    read_file((char *)"__tonglinkq_analysis_file4.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+538, i_len-538);

    i_ret = __tonglinkq_analysis_file(p_st_private, p_st_body_data, p_st_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_L7_session);
    frfree(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_data);
    bytebuff_free(&p_st_data);

    return;
}

//FILE 消息消息长度(513)超过512个字节
static void case5(void)
{
    L7session_tonglinkq_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char *p_data = NULL;
    int i_len = 0;
    int i_ret = FR_SUCC;
    Tonglinkq_L7session *p_st_L7_session = NULL;

    p_st_private = (L7session_tonglinkq_private *)frmalloc(sizeof(L7session_tonglinkq_private));
    memset(p_st_private, 0x00, sizeof(L7session_tonglinkq_private));
    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_L7_session = (Tonglinkq_L7session *)frmalloc(sizeof(Tonglinkq_L7session));
    memset(p_st_L7_session, 0x00, sizeof(Tonglinkq_L7session));
    p_st_private->p_st_L7_session = p_st_L7_session;

    read_file((char *)"__tonglinkq_analysis_file5.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+538, i_len-538);

    i_ret = __tonglinkq_analysis_file(p_st_private, p_st_body_data, p_st_data);

    CU_ASSERT_EQUAL(i_ret, FR_FAIL);

    frfree(p_st_L7_session);
    frfree(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_data);
    bytebuff_free(&p_st_data);

    return;
}

static void case6(void)
{
    L7session_tonglinkq_private *p_st_private = NULL;
    Frreport_body_data *p_st_body_data = NULL;
    Frbyte_Packet *p_st_data = NULL;
    char *p_data = NULL;
    int i_len = 0;
    int i_ret = FR_SUCC;
    Tonglinkq_L7session *p_st_L7_session = NULL;

    p_st_private = (L7session_tonglinkq_private *)frmalloc(sizeof(L7session_tonglinkq_private));
    memset(p_st_private, 0x00, sizeof(L7session_tonglinkq_private));
    p_st_body_data = (Frreport_body_data *)frmalloc(sizeof(Frreport_body_data));
    memset(p_st_body_data, 0x00, sizeof(Frreport_body_data));
    p_st_body_data->em_type = FRREPORT_TYPE_L7DATA;

    p_st_L7_session = (Tonglinkq_L7session *)frmalloc(sizeof(Tonglinkq_L7session));
    memset(p_st_L7_session, 0x00, sizeof(Tonglinkq_L7session));
    p_st_private->p_st_L7_session = p_st_L7_session;

    read_file((char *)"__tonglinkq_analysis_file6.pcap", &p_data, &i_len);

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, p_data+538, i_len-538);

    i_ret = __tonglinkq_analysis_file(p_st_private, p_st_body_data, p_st_data);

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    //CU_ASSERT_STRING_EQUAL(p_st_private->file_name, "file.txt 18115_0_file.txt");
    CU_ASSERT_STRING_EQUAL(p_st_private->file_name, "file.txt");

    frfree(p_st_L7_session);
    frfree(p_st_private);
    frreport_body_free(&p_st_body_data);
    frfree(p_data);
    bytebuff_free(&p_st_data);

    return;
}

int add_suite__tonglinkq_analysis_file(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __tonglinkq_analysis_file()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL ||
        CU_add_test(pSuite, "case4", case4) == NULL ||
        CU_add_test(pSuite, "case5", case5) == NULL ||
        CU_add_test(pSuite, "case6", case6) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
